package com.andyage.admin.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.andyage.admin.bean.AdorderBean;
import com.andyage.admin.service.IAdorderService;
import com.andyage.util.DateUtil;
import com.andyage.util.GetUUID;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/admin")
public class AdorderAction {
	@Resource
	private IAdorderService iadorderService;
	@RequestMapping("/queryAdOrderInfo")
	public void queryAdOrderInfo(AdorderBean order,HttpServletResponse response,HttpServletRequest request) throws Exception {
		String startDate = request.getParameter("sstartDate");
		String endDate = request.getParameter("sendDate");
		if(startDate!=null&&!"".equals(startDate)){
			order.setStartDate(DateUtil.getDate(startDate, "yyyy-MM-dd"));
		}
		if(endDate!=null&&!"".equals(endDate)){
			order.setEndDate(DateUtil.getDate(endDate, "yyyy-MM-dd"));
		}
		String json=iadorderService.queryAdOrderInfo(order);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print(json);
	}
	
	@RequestMapping("/deleteAdOrderInfo")
	public void deleteAdOrderInfo(AdorderBean order,HttpServletResponse response) throws Exception {
		String json=iadorderService.deleteAdOrderInfo(order.getOrderId());
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print(json);
	}
	@RequestMapping("/updateAdorderInfo")
	public void updateAdorderInfo(AdorderBean order,HttpServletResponse response) throws Exception {
		String json=iadorderService.updateAdorderInfo(order);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print(json);
	}
	@RequestMapping("/exportAdorderInfoByCondition")
	public void exportAdorderInfoByCondition(AdorderBean order,HttpServletResponse response) throws Exception {
		HSSFWorkbook wb = iadorderService.exportAdorderInfoByCondition(order);
		String fileName=GetUUID.get();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		wb.write(os);
		
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		// 设置response参数，可以打开下载页面
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String((fileName + ".xls").getBytes(), "utf-8"));
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[20480];
			int bytesRead;
			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (final IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}
	@RequestMapping("/queryChoujiangOrder")
	public void queryChoujiangOrder(AdorderBean order,HttpServletResponse response) throws Exception {
		String json = iadorderService.queryChoujiangOrder(order);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print(json);
	}
	@RequestMapping("/addWinningOrder")
	public void addWinningOrder(AdorderBean order,HttpServletRequest request,HttpServletResponse response) throws Exception {
		String text = request.getParameter("text");//获得输入的抽奖次数
		String json ="";
		if(!"".equals(text)&&null!=text){
			int count = Integer.valueOf(text);
			json = iadorderService.addWinningOrder(order, count);
		}
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print(json);
	}
}
